<template>
  <div class="login_container">
    <div class="mask" ref="maskRef"></div>
    <div class="login_box">
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont el-icon-user-solid"
            @focus="handleInputFocus"
            @blur="handleInputBlur"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont el-icon-lock"
            type="password"
            @focus="handleInputFocus"
            @blur="handleInputBlur"
          ></el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="linkToRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  name: "myLogin",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        username: [
          {
            required: true,
            message: "请输入登录名称",
            trigger: "blur",
          },
          {
            validator: this.checkData,
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符(不能包含中文！)",
            trigger: "blur",
          },
        ],

        password: [
          {
            required: true,
            message: "请输入登录密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapMutations(["setToken", "setMoreUserInfo"]),
    // 点击重置按钮，重置表单
    linkToRegister() {
      this.$router.push("/register");
    },
    login() {
      // 验证表单是否合法
      this.$refs.loginFormRef.validate(async (vaild) => {
        if (!vaild) return;

        const { data: res } = await this.$axios.post("login", this.loginForm);
        // console.log(res);
        if (res.status !== 200) {
          return this.$message.error(res.message);
        }
        const { token } = res;
        const data = res.data[0];
        this.$message.success("登录成功");
        this.setToken({ token });

        let moreUserInfo = {
          username: data.username,
          college: data.college,
          admin: data.admin,
          position: data.position,
          registerTime: data.register_time,
        };
        this.setMoreUserInfo({ moreUserInfo });
        this.$refs.maskRef.style.filter = "blur(0px)";
        // console.log(data);
        // 2、通过变成式导航跳转到后台主页
        setTimeout(() => {
          if (data.admin == 1) {
            console.log("admin");
            this.$router.push({
              path: this.$route.params.redirect || "/admin",
            });
          } else {
            this.$router.push({
              path: this.$route.params.redirect || "/index",
            });
          }
        }, 2200);
      });
    },
    checkData(rule, value, callback) {
      if (value) {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          callback(new Error("不能包含中文!"));
        } else {
          callback();
        }
      }
      callback();
    },
    handleInputFocus() {
      this.$refs.maskRef.style.filter = "blur(3px) brightness(1.1)";
    },
    handleInputBlur() {
      this.$refs.maskRef.style.filter = "blur(0px) brightness(1)";
    },
  },
  async mounted() {
    // let { data: res } = await this.$axios.post('/login');
    // console.log(res);
    // if (res.status == 200) this.$router.push('/index');
  },
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
}

.mask {
  background: url(../assets/dl-login.jpg);
  height: 100%;
  background-size: 100% 100%;
  transition: all 1s;
  // filter:;
}

.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 260px;
  background-color: #fff;
  border-radius: 3px;
}

.fadeIn {
  opacity: 0;
  animation: fadeIn 2s linear forwards;
}

@keyframes fadeIn {
  100% {
    opacity: 1;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: center;
}
</style>
